﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using QQTools.Data.Models;
using QQTools.Data.Plus;
using QQTools.Data.Repositories;
using QQTools.Logging;
using QQTools.Utils;

namespace QQTools.Services
{
    /// <summary>
    /// QQ核心服务
    /// </summary>
    public class QQService
    {
        private readonly QQRepository _repository;
        private readonly MainForm _form;
        public QQService(QQRepository repository, MainForm form)
        {
            _repository = repository;
            _form = form;
        }


        public async Task<List<QQModel>> GetQQList()
        {
            // 获取QQ主窗口的PID和句柄
            Dictionary<int, IntPtr> pidAndHandle = Win32Api.GetPidAndHandle("TXGuiFoundation", "QQ", false);

            List<QQModel> qqList = new List<QQModel>();
            try
            {
                Process[] processes = Process.GetProcessesByName("QQ");
                foreach (Process process in processes)
                {
                    // 1. 获取QQ
                    string qq = QQPlus.GetClientSelfUin(process.Id);
                    LogManager.Info($"[GetQQList] qq:{qq}");
                    // 2. 根据获取昵称
                    string nickname = QQPlus.GetClientName(qq, process.Id);
                    LogManager.Info($"[GetQQList] nickname:{nickname}");

                    // 3. 获取clientKey
                    //string clientKey = QQPlus.GetClientKey(process.Id);
                    //LogManager.Info($"[GetQQList] clientKey:{clientKey}");

                    // 4. 根据ClentKey获取cookie
                    //string cookieByClentKey = QQPlus.GetCookieByClentKey(qq, clientKey);
                    //LogManager.Info($"[GetQQList] cookieByClentKey:{cookieByClentKey}");

                    // 5. 获取httpkey
                    //string httpkey = QQPlus.GetHttpkey(process.Id);
                    //LogManager.Info($"[GetQQList] httpkey:{httpkey}");

                    // 6. 根据httpkey获取cookie
                    //string cookieByHttpkey = QQPlus.GetCookieByHttpKey(qq, httpkey);
                    //LogManager.Info($"[GetQQList] cookieByHttpkey:{cookieByHttpkey}");

                    //// 7. 获取群列表
                    //List<string> groupList = QQPlus.GetGroupList(process.Id);
                    //// 输出群列表
                    //LogManager.Info($"[GetQQList] groupList:{string.Join(",", groupList)}");

                    //qqList.Add(new QQModel
                    //{
                    //    QQ = int.Parse(qq) < 0 ? QQPlus.GetLogInUin(int.Parse(qq)).ToString() : qq.ToString(),
                    //    Nickname = nickname ?? "未知",
                    //    HttpKey = httpkey ?? "",
                    //    ClientKey = clientKey ?? "",
                    //    Cookie = (cookieByHttpkey ?? cookieByClentKey) ?? "",
                    //    GroupNumber = groupList.Count.ToString() ?? "-1",
                    //    Title = "",
                    //    LastHandle = pidAndHandle.ContainsKey(process.Id) ? pidAndHandle[process.Id] : IntPtr.Zero,  // 根据pid从 pidAndHandle 中获取对应的句柄
                    //    LastPid = process.Id,
                    //    GroupList = groupList
                    //});


                    // 7. 获取群列表
                    List<string> groupList = QQPlus.GetGroupList(process.Id);
                    // 输出群列表
                    LogManager.Info($"[GetQQList] groupList:{string.Join(",", groupList)}");
                    qq = int.Parse(qq) < 0 ? QQPlus.GetLogInUin(int.Parse(qq)).ToString() : qq.ToString();
                    qqList.Add(new QQModel
                    {
                        QQ = qq,
                        Nickname = nickname ?? "未知",
                        HttpKey = "",
                        ClientKey = "",
                        Cookie = "",
                        GroupNumber = groupList.Count.ToString() ?? "-1",
                        Title = "",
                        LastHandle = pidAndHandle.ContainsKey(process.Id) ? pidAndHandle[process.Id] : IntPtr.Zero,  // 根据pid从 pidAndHandle 中获取对应的句柄
                        LastPid = process.Id,
                        GroupList = groupList
                    });
                    //// 等待1秒, 防止频繁操作
                    _form.UpdateStatusStrip($"[获取QQ信息] 当前QQ号:{qq}({nickname}) 群数:{groupList.Count}");
                    await Task.Delay(1500);
                }
                _form.UpdateStatusStrip($"[获取QQ信息] 获取完毕, 在线QQ号:{qqList.Count} 个");
            }
            catch (Exception ex)
            {
                LogManager.Error($"[GetQQList] 获取QQ列表异常:{ex.Message}", ex);
            }
            return qqList;
        }
    }
}
